package com.ruoyi.project.tickets.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.tickets.domain.dto.ShopUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.tickets.mapper.ShopUserMapper;
import com.ruoyi.project.tickets.domain.ShopUser;
import com.ruoyi.project.tickets.service.IShopUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 店铺用户信息Service业务层处理
 * 
 * @author 孙山伟
 * @date 2023-06-28
 */
@Service
public class ShopUserServiceImpl implements IShopUserService 
{
    @Autowired
    private ShopUserMapper shopUserMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询店铺用户信息
     * 
     * @param id 店铺用户信息ID
     * @return 店铺用户信息
     */
    @Override
    public ShopUser selectShopUserById(Long id)
    {
        return shopUserMapper.selectShopUserById(id);
    }

    /**
     * 查询店铺用户信息列表
     * 
     * @param shopUser 店铺用户信息
     * @return 店铺用户信息
     */
    @Override
    public List<ShopUser> selectShopUserList(ShopUser shopUser)
    {
        return shopUserMapper.selectShopUserList(shopUser);
    }

    /**
     * 新增店铺用户信息
     * 
     * @param dto 店铺用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertShopUser(ShopUserDto dto)
    {
        /*先删除原有的*/
       shopUserMapper.deleteByShopId(dto.getShopId());
       String []ids=dto.getUserId().split(",");
       for(String id:ids){
           ShopUser shopUser= new ShopUser();
           shopUser.setShopId(dto.getShopId());
           shopUser.setUserId(Long.valueOf(id));
           shopUser.setCreateTime(DateUtils.getNowDate());
           shopUserMapper.insertShopUser(shopUser);
       }

        return 1;
    }

    /**
     * 修改店铺用户信息
     * 
     * @param shopUser 店铺用户信息
     * @return 结果
     */
    @Override
    public int updateShopUser(ShopUser shopUser)
    {
        shopUser.setUpdateTime(DateUtils.getNowDate());
        return shopUserMapper.updateShopUser(shopUser);
    }

    /**
     * 批量删除店铺用户信息
     * 
     * @param ids 需要删除的店铺用户信息ID
     * @return 结果
     */
    @Override
    public int deleteShopUserByIds(Long[] ids)
    {
        return shopUserMapper.deleteShopUserByIds(ids);
    }

    /**
     * 删除店铺用户信息信息
     * 
     * @param id 店铺用户信息ID
     * @return 结果
     */
    @Override
    public int deleteShopUserById(Long id)
    {
        return shopUserMapper.deleteShopUserById(id);
    }

    @Override
    public Long getShopIdBySystemUser() {
        LoginUser user = tokenService.getLoginUser(ServletUtils.getRequest());
        ShopUser shopUser = new ShopUser();
        shopUser.setUserId(user.getUser().getUserId());
        List<ShopUser> shopUserList =selectShopUserList(shopUser);
        if (ObjectUtil.isNull(shopUserList)) {
            shopUser.setShopId(-1L);
        } else {
            if (shopUserList.size() == 0) {
                shopUser.setShopId(-1L);
            } else {
                shopUser.setShopId(shopUserList.get(0).getShopId());
            }
        }
        return shopUser.getShopId();
    }
}
